/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fjnu.dao.mysql;

import fjnu.dao.AbstractObject;
import fjnu.dao.Bulletinboard;
import fjnu.dao.factory.AbstractBulletinBoardMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author neco
 */
public class MysqlBulletinBoardMapper extends AbstractBulletinBoardMapper {

    private static final String readSQL = "SELECT * FROM fjnu.bulletinboard WHERE ID=";
    private static final String readTypeSQL = "SELECT * FROM fjnu.bulletinboarddet WHERE Type='";
    private static final String FixSQL = "UPDATE `fjnu`.`bulletinboard` SET `Broken`=0 WHERE `ID`=";
    private static final String BrokenSQL = "UPDATE `fjnu`.`bulletinboard` SET `Broken`=`Broken`+1 WHERE `ID`=";
    private static final String readBorkenSQL = "SELECT ID FROM fjnu.bulletinboard WHERE Broken>0;";
    
    @Override
    public AbstractObject getObjectByID(int ID) {
        Bulletinboard mbulletinboard = new Bulletinboard();
        MysqlStatement mysqlState = new MysqlStatement();
        try {
               ResultSet rs = mysqlState.excuteSQL(readSQL + ID+";");
            mbulletinboard = BaseMapper(rs, mbulletinboard);
               ResultSet rs2 = mysqlState.excuteSQL(readTypeSQL + mbulletinboard.getType()+"';");
            mbulletinboard = ExMapper(rs2, mbulletinboard);

        } catch (SQLException ex) {
            Logger.getLogger(MysqlBulletinBoardMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.addObjectToCache(mbulletinboard);
        return mbulletinboard;
    }

    @Override
    public void fix(AbstractObject o, Object arg) {
        MysqlStatement mysqlState = new MysqlStatement();
        mysqlState.excuteSQLNotResult(FixSQL + o.getID()+";");
    }

    @Override
    public void broken(AbstractObject o, Object arg) {
        MysqlStatement mysqlState = new MysqlStatement();
        mysqlState.excuteSQLNotResult(BrokenSQL + o.getID()+";");
    }

    public Bulletinboard BaseMapper(ResultSet rs, Bulletinboard bulletinboard) throws SQLException {
        if (rs.next()) {
            bulletinboard.setID(rs.getInt("ID"));
            bulletinboard.setType(rs.getString("Type"));
            Date date=  (java.util.Date) rs.getDate("BuyTime");
	    bulletinboard.setBelonging(rs.getString("Belonging"));
            bulletinboard.set_Broken(rs.getInt("Broken"));
            bulletinboard.setBuyTime(date);
        }
        return bulletinboard;
    }

    public Bulletinboard ExMapper(ResultSet rs, Bulletinboard bulletinboard) throws SQLException {
        if (rs.next()) {
            bulletinboard.setName(rs.getString("Name"));
            bulletinboard.setMaterial(rs.getString("Material"));
            bulletinboard.setPrice(rs.getFloat("Price"));
            bulletinboard.setSupplier(rs.getString("Supplier"));         
        }
        return bulletinboard;
    }

    @Override
    public  ArrayList<Integer> getBrokenList() {
         ArrayList<Integer> result = new  ArrayList<Integer>();
        try {
            MysqlStatement mysqlState = new MysqlStatement();
            ResultSet rs = mysqlState.excuteSQL(readBorkenSQL);
            if(rs==null)
                System.err.println("FAil");
            while (rs.next()) {
                    System.err.println(rs.getRow());
                result.add(rs.getInt("ID"));
              
            }
        } catch (SQLException ex) {
            Logger.getLogger(MysqlBulletinBoardMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

    @Override
    public AbstractObject getObjectByIDNotCache(int ID) {
         Bulletinboard mBulletinBoard = new Bulletinboard();
        MysqlStatement mysqlState = new MysqlStatement();
        try {
            ResultSet rs = mysqlState.excuteSQL(readSQL + ID + ";");
            mBulletinBoard = BaseMapper(rs, mBulletinBoard);
            ResultSet rs2 = mysqlState.excuteSQL(readTypeSQL + mBulletinBoard.getType() + "';");
            mBulletinBoard = ExMapper(rs2, mBulletinBoard);

        } catch (SQLException ex) {
            Logger.getLogger(MysqlBulletinBoardMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
      //  this.addObjectToCache(mBulletinBoard);
        return mBulletinBoard;
    }
}
